package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.ui.DropShadowBorder;
import ac.svn.accesscontrol.ui.IUIConstants;
import ac.svn.accesscontrol.ui.JPanel;
import ac.svn.accesscontrol.ui.JScrollPane;
import ac.svn.accesscontrol.ui.MarginBorder;
import ac.svn.accesscontrol.ui.TitledBorder;
import ac.svn.accesscontrol.ui.UIComponentLibrary;
import ac.svn.accesscontrol.ui.XMenuBar;
import ac.svn.accesscontrol.ui.model.ThreadedListModel;
import ac.svn.accesscontrol.ui.panel.RToolbar;
import ac.svn.accesscontrol.ui.renderer.AccessListCellRenderer;
import ac.svn.accesscontrol.ui.renderer.LocationListCellRenderer;
import ac.svn.accesscontrol.ui.renderer.UserTreeCellRenderer;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

public class MainWindowI extends JFrame implements IUIConstants 
{
	/**
	 * 
	 */
	 private static final long serialVersionUID = 1L;

	 private XMenuBar menuBar;
	 private JToolBar statusBar;
	 private JLabel statusLabel;
	 private JLabel errorLabel;
	 private RToolbar toolBar;
	 private JPanel basePanel;
	 private JList locationListView;
	 private ThreadedListModel locationListModel;
	 private JList accessListView;
	 private ThreadedListModel  accessListModel;
	 private JScrollPane treeScrollPane;
	 private JTree svnTree;
	 private DefaultMutableTreeNode rootNode;
	 private JTextField userSearchTxt;
	 private JTextField locationSearchTxt;
	 private JSplitPane splitPane;
	 private JScrollPane scrpLocations; 
	 private JTabbedPane tabbedPane;
	 private javax.swing.JScrollPane targetItemScrollPane;
	 private JList targetItemListView;

	 //private JTable tableView;

	 public MainWindowI()
	 {
		 super("Smart CM");
		 initComponents();
		 registerComponents();
		 //setSize(600,400);
		// setExtendedState(JFrame.MAXIMIZED_BOTH);
	 }

	 private void registerComponents() {
		 UIComponentLibrary.addComponent(C_MAIN_WINDOW, this);
		 UIComponentLibrary.addComponent(C_MENU_BAR, menuBar);
		 UIComponentLibrary.addComponent(C_STATUS_BAR, statusBar);
		 UIComponentLibrary.addComponent(C_STATUS_LABEL, statusLabel);
		 UIComponentLibrary.addComponent(C_ERROR_LABEL,errorLabel);
		 UIComponentLibrary.addComponent(C_TOOL_BAR, toolBar);
		 UIComponentLibrary.addComponent(C_LOCATION_MODEL,locationListModel);
		 UIComponentLibrary.addComponent(C_LOCATION_LIST,locationListView);
		 UIComponentLibrary.addComponent(C_ACCESS_MODEL,accessListModel);
		 UIComponentLibrary.addComponent(C_ACCESS_LIST,accessListView);
		 UIComponentLibrary.addComponent(C_USERS_TREE,svnTree);
		 UIComponentLibrary.addComponent(C_ROOT_NODE,rootNode);
		 UIComponentLibrary.addComponent(C_TEXT_S_LOCATION,locationSearchTxt);
		 UIComponentLibrary.addComponent(C_TEXT_S_USER,userSearchTxt);
		 UIComponentLibrary.addComponent(C_LOCATION_LIST_VIEW_HOLDER,splitPane);
	 }

	 private void initComponents() {
		 getContentPane().setLayout(new BorderLayout(5,0));

		 menuBar = new XMenuBar();
		 setJMenuBar(menuBar);
		 setBackground(Color.WHITE);

		 statusLabel = new JLabel("Welcome to Smart CM");
		 errorLabel = new JLabel("Issues: 0");
		 errorLabel.setForeground(Color.RED);

		 statusBar = new JToolBar();
		 statusBar.add(statusLabel);
		 statusBar.addSeparator();
		 statusBar.add(errorLabel);
		 statusBar.addSeparator();
		 statusBar.setFloatable(false);
		 statusBar.setBackground(Color.WHITE);
		 statusBar.setPreferredSize(new Dimension(300,40));
		 UIUtility.setAllShadowedBorder(statusBar);

		 getContentPane().add(statusBar,BorderLayout.SOUTH);

		 toolBar = new RToolbar();
		 toolBar.setFloatable(false);

		 //JScrollPane tsp = new JScrollPane(toolBar);
		 getContentPane().add(toolBar,BorderLayout.NORTH);

		 basePanel = new JPanel(new BorderLayout(5, 5),true);
		 //UIUtility.setAllShadowedBorder(basePanel);

		 rootNode = new DefaultMutableTreeNode("Users");
		 svnTree = new JTree(rootNode);
		 svnTree.setDragEnabled(true);

		 svnTree.setBorder(UIManager.getBorder("TextField.border"));
		 svnTree.setCellRenderer(new UserTreeCellRenderer());
		 svnTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
		 ToolTipManager.sharedInstance().registerComponent(svnTree);
		 treeScrollPane = new JScrollPane(svnTree);
		 treeScrollPane.setPreferredSize(new Dimension(300, 700));
		 treeScrollPane.setBorder(new TitledBorder("Groups  [Search: F3]"));
		 treeScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		 basePanel.add(treeScrollPane,BorderLayout.WEST);
		 //svnTree.setScrollsOnExpand(true);

		 JScrollPane scrollPane;

		 new JPanel((new BorderLayout(5, 5)),true);
		 splitPane 	 	= new JSplitPane(JSplitPane.VERTICAL_SPLIT);

		 splitPane.setBackground(Color.WHITE);
		 splitPane.setBorder(new TitledBorder("SVN"));

		 locationListModel = new ThreadedListModel();
		 locationListView = new JList(locationListModel);
		 locationListView.setBorder(UIManager.getBorder("TextField.border"));

		 //SimpleIconRenderer iconRenderer = new SimpleIconRenderer(IResources.LOC_ICN_URL);
		 //iconRenderer.setTextFormatter(new LocationItemNameFormatter());
		 locationListView.setCellRenderer(new LocationListCellRenderer());

		 scrpLocations = new JScrollPane(locationListView);
		 scrpLocations.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		 scrpLocations.setBorder(new TitledBorder("Locations [Search: F4]"));
		 scrpLocations.setPreferredSize(new Dimension(500, 300));
		 //bodyPanel.add(scrollPane,BorderLayout.CENTER);
		 splitPane.setTopComponent(scrpLocations);

		 accessListModel = new ThreadedListModel();
		 accessListView  = new JList(accessListModel);
		 accessListView.setCellRenderer(new AccessListCellRenderer());
		 scrollPane = new JScrollPane(accessListView);
		 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		 scrollPane.setBorder(new TitledBorder("Rights"));
		 scrollPane.setPreferredSize(new Dimension(500, 200));
		 //bodyPanel.add(scrollPane,BorderLayout.SOUTH);
		 splitPane.setBottomComponent(scrollPane);

		 splitPane.addComponentListener(new ComponentAdapter() {
			 @Override
			 public void componentResized(ComponentEvent arg0) {
				 JSplitPane s = (JSplitPane) arg0.getSource();
				 s.setDividerLocation(.75);
			 }
		 });

		 userSearchTxt = new JTextField();
		 locationSearchTxt = new JTextField();
		 
		 addUserSearchComponent(userSearchTxt);
		 addLocationSearchComponent(locationSearchTxt);

		 //bodyPanel.add(splitPane,BorderLayout.CENTER);
		 //basePanel.add(splitPane,BorderLayout.EAST);

		 getContentPane().add(basePanel,BorderLayout.CENTER);
		 
		 tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		 basePanel.add(tabbedPane, BorderLayout.CENTER);
		 
		 tabbedPane.addTab("Location",Utility.getImageIcon(IResources.LOC_ICN_URL),splitPane);
		 
		 targetItemScrollPane = new javax.swing.JScrollPane();
		 targetItemScrollPane.setPreferredSize(new Dimension(100, 2));
		 basePanel.add(targetItemScrollPane, BorderLayout.EAST);
		 
		 targetItemListView = new JList();
		 targetItemScrollPane.setViewportView(targetItemListView);
		 
		 registerListeners();
		
	 }

	 private void registerListeners()
	 {
		 userSearchTxt.addFocusListener(new FocusListener() {
			 @Override
			 public void focusLost(FocusEvent e) {
				 JComponent component = (JComponent) e.getSource();
				 component.setBorder((new JTextField()).getBorder());
			 }

			 @Override
			 public void focusGained(FocusEvent e) {
				 JComponent component = (JComponent) e.getSource();
				 component.setBorder(new DropShadowBorder(Color.GREEN,5,0.7f,7,true,true,true,true));
			 }
		 });
		 
		 locationSearchTxt.addFocusListener(new FocusListener() {
			 @Override
			 public void focusLost(FocusEvent e) {
				 JComponent component = (JComponent) e.getSource();
				 component.setBorder((new JTextField()).getBorder());
			 }

			 @Override
			 public void focusGained(FocusEvent e) {
				 JComponent component = (JComponent) e.getSource();
				 component.setBorder(new DropShadowBorder(Color.GREEN,5,0.7f,7,true,true,true,true));
			 }
		 });		
	 }
	 
	 
	 public void setLocation() {
		 Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		 setLocation((int)(size.getWidth()/2 - getWidth()/2),(int)(size.getHeight()/2-getHeight()/2));
	 }

	 public void addLocationSearchComponent(Component component)
	 {
		 JPanel searchPanel = new JPanel((new BorderLayout(5,2)),true);
		 searchPanel.setBorder(new MarginBorder());
		 JLabel searchLabel = new JLabel(Utility.getImageIcon(IResources.LOC_ICN_SEARCH));
		 searchLabel.setText("Search");
		 searchPanel.add(searchLabel,BorderLayout.WEST); 
		 searchPanel.add(component,BorderLayout.CENTER);
		 scrpLocations.setColumnHeaderView(searchPanel);
	 }

	 public void addUserSearchComponent(Component component)
	 {
		 JPanel searchPanel = new JPanel((new BorderLayout(5,2)),true);
		 searchPanel.setBorder(new MarginBorder());
		 JLabel searchLabel = new JLabel(Utility.getImageIcon(IResources.LOC_ICN_SEARCH));
		 searchLabel.setText("Search");
		 searchPanel.add(searchLabel,BorderLayout.WEST); 
		 searchPanel.add(component,BorderLayout.CENTER);
		 treeScrollPane.setColumnHeaderView(searchPanel);
	 }

	 public static void main(String args[]) throws Exception
	 {
		 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		 MainWindow mainWindow = new MainWindow();
		 mainWindow.setVisible(true);
	 }   

	 public void addLocationView(Component component)
	 {
		 if(null != splitPane)
		 {
			 basePanel.add(component,BorderLayout.CENTER);
			 SwingUtilities.invokeLater(new Runnable() {
				 @Override
				 public void run() {
					 SwingUtilities.updateComponentTreeUI(basePanel);
				 }
			 });
		 }
	 }    

	 public JScrollPane getLocationListView()
	 {
		 return scrpLocations;
	 }

	 @Override
	 public void setVisible(boolean arg0) {
		 super.setVisible(arg0);
	 }

	 public void addTab(String tabName,Icon icon,Component component )
	 {
		 tabbedPane.addTab(tabName, icon,component);
	 }
	 

}



